/*
Copyright (c) 2013, Le Tallec Jean-Francois

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "quotationHandler.h"

quotationHandler::quotationHandler():initDone(false){
	srand(time(NULL));
}

quotationHandler::quotationHandler(std::string xmlFile):m_xmlFile(xmlFile),initDone(false){
	srand(time(NULL));
	loadQuotationFile(m_xmlFile);
}
quotationHandler::~quotationHandler(){
	serialize();
}

void quotationHandler::loadQuotationFile(std::string xmlFile){
	xml_schema::properties props;
	props.schema_location ("http://www.example.org/quotation", m_quotationXsdFile.getXsdFileName());
	m_q = quotation::root_ (xmlFile, 0, props);
	initDone=true;
}

quotation::quotes quotationHandler::getRandomQuotes(){
	if(!initDone){throw commonException(this, " no data base loaded ! loadQuotationFile(std::string xmlFile) must be call once before using other methods");}
	int randomNumber = (int)((double)rand() / ((double)RAND_MAX + 1) * howManyQuotes());
	quotation::root::quotes_sequence& qs ( m_q->quotes () );
	return qs.at(randomNumber);
}

quotation::quotes quotationHandler::getRandomQuotesFromAuthor(std::string authorName){
	if(!initDone){throw commonException(this, " no data base loaded ! loadQuotationFile(std::string xmlFile) must be call once before using other methods");}
	std::vector<size_t> authorQuotesReference;
	quotation::root::quotes_sequence& qs ( m_q->quotes () );
	for (size_t i=0; i<qs.size(); i++){
		quotation::quotes& q (qs.at(i));
		if(q.author() == authorName){
			authorQuotesReference.push_back(i);
		}
	}
	if(authorQuotesReference.size()==0){
		std::cout << "no author \"" << authorName << "\" in the database" << std::endl;
		exit(0);
	}
	int randomNumber = (int)((double)rand() / ((double)RAND_MAX + 1) * authorQuotesReference.size());
	return qs.at(authorQuotesReference.at(randomNumber));
}

void quotationHandler::printRandomQuoteFromAuthor(std::string authorName){
	if(!initDone){throw commonException(this, " no data base loaded ! loadQuotationFile(std::string xmlFile) must be call once before using other methods");}
	quotation::quotes q=getRandomQuotesFromAuthor(authorName);
	std::cout << q.author() << " said: " << q.quote() << std::endl;
}

void quotationHandler::printRandomQuoteFromAuthorWithUI(){
	if(!initDone){throw commonException(this, " no data base loaded ! loadQuotationFile(std::string xmlFile) must be call once before using other methods");}
	std::string author;
	std::cout << "Author's name: ";
	std::getline (std::cin,author);
	printRandomQuoteFromAuthor(author);
}

void quotationHandler::printRandomQuote(){
	if(!initDone){throw commonException(this, " no data base loaded ! loadQuotationFile(std::string xmlFile) must be call once before using other methods");}
	quotation::quotes q=getRandomQuotes();
	std::cout << q.author() << " said: " << q.quote() << std::endl;
}

void quotationHandler::listAuthors(){
	if(!initDone){throw commonException(this, " no data base loaded ! loadQuotationFile(std::string xmlFile) must be call once before using other methods");}
	std::vector<std::string> Authors = getAuthorsVector();
	std::cout << "List of all authors:" << std::endl;
	for(size_t i=0; i<Authors.size(); i++){
		std::cout << Authors[i] << std::endl;
	}
}

void quotationHandler::addQuotesWithUserInterface(){
	if(!initDone){throw commonException(this, " no data base loaded ! loadQuotationFile(std::string xmlFile) must be call once before using other methods");}
	std::string author;
	std::string quote;
	std::cout << "Author's name: ";
	std::getline (std::cin,author);
	std::cout << "Quote: ";
	std::getline (std::cin,quote);
	quotation::quotes q(author,quote);
	addQuotes(q);
}

void quotationHandler::addQuotes(quotation::quotes q){
	if(!initDone){throw commonException(this, " no data base loaded ! loadQuotationFile(std::string xmlFile) must be call once before using other methods");}
	quotation::root::quotes_sequence& qs ( m_q->quotes () );
	qs.push_back(q);
}

size_t quotationHandler::howManyQuotes(){
	if(!initDone){throw commonException(this, " no data base loaded ! loadQuotationFile(std::string xmlFile) must be call once before using other methods");}
	quotation::root::quotes_sequence& qs ( m_q->quotes () );
	return qs.size();
}

std::vector<std::string> quotationHandler::getAuthorsVector(){
	if(!initDone){throw commonException(this, " no data base loaded ! loadQuotationFile(std::string xmlFile) must be call once before using other methods");}
	std::vector<std::string> Authors;
	quotation::root::quotes_sequence& qs ( m_q->quotes () );
	for (quotation::root::quotes_iterator i (qs.begin ()); i != qs.end (); ++i){
		quotation::quotes& q (*i);
		Authors.push_back(q.author ());
	}
	std::sort(Authors.begin(), Authors.end());
	std::vector<std::string>::iterator it = std::unique(Authors.begin(), Authors.end());
	Authors.resize( std::distance(Authors.begin(),it) );
	return Authors;
}	

size_t quotationHandler::howManyAuthors(){
	if(!initDone){throw commonException(this, " no data base loaded ! loadQuotationFile(std::string xmlFile) must be call once before using other methods");}
	return getAuthorsVector().size();
}

void quotationHandler::serialize(){
	if(!initDone){throw commonException(this, " no data base loaded ! loadQuotationFile(std::string xmlFile) must be call once before using other methods");}
	xml_schema::namespace_infomap map;
	map[""].name = "http://www.example.org/quotation";
	map[""].schema = m_quotationXsdFile.getXsdFileName();
	std::ofstream ofs (m_xmlFile.c_str());
	quotation::root_ (ofs, *m_q, map);
}
